import requests
from typing import Union

class MCPClient:
    def __init__(self, base_url: str = "http://localhost:8000"):
        self.base_url = base_url


    def check_prime(self, number: int) -> dict:
        """检查素数"""
        response = requests.post(
            f"{self.base_url}/prime_check",
            json={"number": number}
        )
        response.raise_for_status()
        return response.json()

    def get_greeting(self, name: str) -> dict:
        """获取问候语"""
        response = requests.get(
            f"{self.base_url}/resources/greeting/{name}"
        )
        response.raise_for_status()
        return response.json()

if __name__ == "__main__":
    client = MCPClient()

    try:
        # 测试素数检查
        print("\n测试素数检查:")
        test_numbers = [2, 17, 25, 29, 100]
        for num in test_numbers:
            result = client.check_prime(num)
            print(f"{num}: {'是素数' if result['is_prime'] else '不是素数'}")

        # 测试问候接口
        print("\n测试问候接口:")
        print(client.get_greeting("Alice")["result"])

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {e}")
    except KeyError as e:
        print(f"响应数据格式错误: {e}")